/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param lists ListNode类vector
     * @return ListNode类
     */
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<int, vector<int>, greater<int>> q;
        for (auto e : lists) {
            while (e) {
                q.push(e->val);
                e = e->next;
            }
        }
        ListNode* head = nullptr, *tail = nullptr;
        while (q.size()) {
            int t = q.top();
            if (head == nullptr) {
                head = new ListNode(t);
                tail = head;
            } else {
                ListNode* newnode = new ListNode(t);
                tail->next = newnode;
                tail = newnode;
            }
            q.pop();
        }
        return head;
    }
};